<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="商品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >{{ $t('search') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('reset') }}</el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['good:brand:add']"
        >新增</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="listList">
      <el-table-column label="ID" align="center" prop="id" width="100" />
      <el-table-column label="商品种类" align="center" prop width="100">
        <template slot-scope="scope">
          <span>普通商品</span>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="left" prop="name" width="600" show-overflow-tooltip />
      <el-table-column label="商品图片" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <oss-image-preview
            v-if="scope.row.image"
            :src="scope.row.image"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column label="售价" align="center" prop="price" :formatter="formatter" />
      <el-table-column label="商品库存" align="center" prop="num" />
      <el-table-column label="上架" align="center" prop="isMarketable">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isMarketable"
            :active-value="1"
            :inactive-value="0"
            @change="marketableChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column :label="$t('operate')" align="center" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-search" @click="view(scope.row)">查看详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >{{ $t('edit') }}</el-button>
          <el-button
            v-if="scope.row.isMarketable == '0'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >{{ $t('delete') }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { addList, delList, listList, marketable, updateList } from "@/api/good/xiapi-list"
import OssImagePreview from "@/components/ImagePreview/oss-image-preview.vue"
import DictSelect from "@/components/DictSelect/index.vue"

export default {
  name: "xiapi-list",
  components: { DictSelect, OssImagePreview },
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品列表表格数据
      listList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      queryParams: {
        isDelete: 0,
        page: 1,
        pageSize: 10,
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created () {
    this.getList()
  },
  methods: {
    /** 查询商品列表列表 */
    async getList () {
      this.loading = true
      const response =
        // await mock('/good/xiapi-list/xiapi-list')
        await listList(this.dto)
      this.listList = response.data.records
      console.log('this.listList', this.listList)
      this.total = response.data.total
      this.loading = false
      // });
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.form = {
        id: null,
        shopId: null,
        itemId: null,
        name: null,
        catIdArr: null,
        language: null,
        image: null,
        images: null,
        videos: null,
        sellPrice: null,
        description: null,
        createTime: null,
        updateTime: null,
        stocks: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.$router.push('add')

      // this.reset();
      // this.open = true;
      // this.title = "添加商品列表";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.$router.push('/good/edit/' + row.id)
    },
    view (row) {
      this.$router.push('/good/view/' + row.id)
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateList(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addList(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids
      this.$modal.confirm(this.$t('delete-or-not')).then(function () {
        return delList(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess(this.$t('delete-success'))
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('good/xiapi-list/export', {
        ...this.queryParams
      }, `list_${new Date().getTime()}.xlsx`)
    },
    formatter (row) {
      return this.$yun.priceFormatter(row.price)
    },
    marketableChange (row) {
      marketable(row.id).then(rep => {
        this.$modal.msgSuccess("设置成功")
        this.getList()
      })
    }
  },
  computed: {
    dto () {
      const { page, pageSize, name, isDelete } = this.queryParams
      return {
        current: page,
        size: pageSize,
        records: [{
          name,
          isDelete,
        }]
      }
    }
  }
};
</script>
